<?php
require_once '../DTO/SanPham.php';
require_once 'DBConnection.php';

class SanPhamDAO extends DBConnection{
	public function __construct(){
		parent::__construct();
	}
	
	//Lấy danh sách tất cả các sản phẩm
	public function getSanPham(){
		$sql = "select * from sanpham";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while($temp){
				$objs[] = new SanPham($temp['MaSP'], $temp['TenSP'], $temp['MaDMSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm sản phẩm theo mã sản phẩm
	public function getSanPhamByMaSP($maSP){
		$sql = "select * from sanpham where MaSP='$maSP'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$temp = mysql_fetch_assoc($res);
			$obj = new SanPham($temp['MaSP'], $temp['TenSP'], $temp['MaDMSP']);
			return $obj;
		}else{
			return null;
		}
	}
	
	//Tìm sản phẩm theo mã danh mục sản phẩm
	public function getSanPhamByMaDMSP($maDMSP){
		$sql = "select * from sanpham where MaDMSP='$maDMSP'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new SanPham($temp['MaSP'], $temp['TenSP'], $temp['MaDMSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Insert into sanpham
	public function insert(SanPham $sanPham){
		$tenSP = $sanPham->getTenSP();
		$maDMSP = $sanPham->getMaDMSP();
		
		$sql = "insert into sanpham (TenSP, MaDMSP) values ('$tenSP', '$maDMSP')";
		
		$res = mysql_query($sql);
		
		if(!$res){
			return NULL;
		}
	}
}














